#!/bin/bash
#for mini-gwac star guider test
DIR_data=`pwd`
tempmatchstars=ref.cat
referimage=ref.fit
pixelscale=0.51 #arcsec
xyradius=40 # about 20 arcsec error 
dir_basicimage=/workspace/basicfile

#================================================
xMountTrackplot ( )
{
cat -n newxyshift.cat.all >newxyshift.cat.all.plot
tail -1 newxyshift.cat.all.plot >newxyshift.cat.plot
#IDccdNum=`echo $FITFILE | cut -c4-5`
#ra1=`gethead $FITFILE "RA"`
#dec1=`gethead $FITFILE "DEC" `
#ra_mount=`skycoor -d $ra1 $dec1 | awk '{printf("%.1f\n",$1)}'`
#dec_mount=`skycoor -d $ra1 $dec1 | awk '{printf("%.1f\n",$2)}'`
#ccdid=`gethead $FITFILE "CCDID"`
#echo "DEC of the mount is: " $dec_mount

#pngfile=`echo "MG_"$IDccdNum"_"$ra_mount"_"$dec_mount".png"`
#pngtitle=`echo "MG_"$IDccdNum"_"$ra_mount"_"$dec_mount`
#echo "pngfile="$pngfile "ra="$ra_mount "dec="$dec_mount
pngfile=60cmTrack.png
gnuplot << EOF
set term png
set output "$pngfile"
set xlabel "Images"
set ylabel "Delta pixels relative to ref image"
set grid
set key left
set key box
#set title "$pngtitle"
f(x)=0
plot 'newxyshift.cat.all.plot' u 1:2 w lp pt 6 ps 2 title 'DeltaX','newxyshift.cat.all.plot' u 1:3 w lp pt 8 ps 2 title 'DeltaY','newxyshift.cat.plot' u 1:2 w p pt 6 ps 3 title '','newxyshift.cat.plot' u 1:3 w p pt 8 ps 3 title '',f(x) w l lt -1 lw 1 title 'Ref'
reset
quit
EOF
displayPadNum=`ps -all | awk '{if($14=="display") print($4)}'`
kill -9 $displayPadNum
display -resize 500x500+0+0 $pngfile &
}


xMountTrack ( )
{
        case $ccdid in
        A | C | E | G | I | K )
        CCD_set=South;;
        B | D | F | H | J | L )
        CCD_set=North;;
        esac
        xshiftG=`cat newxyshift.cat | awk '{print(-1*$1)}'`
        yshiftG=`cat newxyshift.cat | awk '{print(-1*$2)}'`
	echo "temp relative to the new image:  xshift="$xshiftG  "  yshift=" $yshiftG 
        if [ $CCD_set == "South"  ] # south CCD
        then
                echo "South CCD"
                deltapixel=`echo $pixelscale $dec_mount | awk '{print($1/cos($2+10))}'`
                if [ ` echo " $yshiftG > 0 " | bc ` -eq 1 ]
                then
			echo "To west: new image relative to temp"
                        RA_guider=+
                else
			echo "To east: new image relative to temp"
                        RA_guider=-
                        yshiftG=`echo $yshiftG | awk '{print(-1*$1)}'`
                fi
                if [ `echo " $xshiftG > 0"  | bc ` -eq 1 ]
                then
			echo "To north: new image relative to temp"
                        DEC_guider=+
                else
			echo "To south: new image relative to temp"
                        DEC_guider=-
                        xshiftG=`echo $xshiftG | awk '{print(-1*$1)}'`
                fi
        else # north CCD
                echo "North CCD"
                deltapixel=`echo $pixelscale $dec_mount | awk '{print($1/cos($2-10))}'`
                if  [ ` echo " $yshiftG > 0 " | bc ` -eq 1 ]
                then
			echo "To east: new image relative to temp"
                        RA_guider=-
                else
			echo "To west: new image relative to temp"
                        RA_guider=+
			yshiftG=`echo $yshiftG | awk '{print(-1*$1)}'`
                fi

                if [ `echo " $xshiftG > 0"  | bc ` -eq 1 ]
                then
			echo "To south: new image relative to temp"
                        DEC_guider=-
                else
			echo "To north: new image relative to temp"
                        DEC_guider=+
                        xshiftG=`echo $xshiftG | awk '{print(-1*$1)}'`
                fi
        fi
        xshiftG_sky=`echo $xshiftG $pixelscale | awk '{printf("%04d\n",$1*$2)}'` # dec axis no any projection relative to the mount point (DEC)
	yshiftG_sky=`echo $yshiftG $deltapixel | awk '{printf("%04d\n",$1*$2)}'` # ra axis 
        echo $RA_guider $yshiftG $DEC_guider $xshiftG
	RADECmsg_sky_tmp=`echo "d#"$IDccdNum"bias"$RA_guider$yshiftG_sky$DEC_guider$xshiftG_sky`
        guidertime=`date +%Y%m%d%H%M%S | cut -c3-14 | awk '{print($1"%")}'`
        RADECmsg_sky=`echo $RADECmsg_sky_tmp$guidertime`
        echo "new image relative to the temp in arcsec: " $RADECmsg_sky
        echo $RADECmsg_sky >>listmsgforHuang
        echo $RADECmsg_sky >listmsgforHuang.last.cat
	date
        ./xsentshift #sent the shift values to telescope controlers.  
#        rm -rf listmsgforHuang.last.cat
}
ximcombine ( )
{
cd $HOME/iraf
cp -f login.cl.old login.cl
echo noao >> login.cl
echo digiphot >> login.cl
echo daophot >>login.cl
echo "cd $DIR_data" >> login.cl
echo flpr >> login.cl
echo "imcombine(\"@comlist\",\"$comimage\", reject=\"crreject\",combine=\"average\",scale=\"exposure\",weight=\"exposure\",rdnoise=$rdnoise,gain=$gain)" >>login.cl
echo logout >>login.cl
cl < login.cl >xlogfile
cd $DIR_data
comimage_objcat=`echo $comimage | sed 's/\.fit/.fit.sex/'` 
sex $comimage  -c  daofind.sex  -DETECT_THRESH 5 -ANALYSIS_THRESH 5 -CATALOG_NAME $comimage_objcat
}

xSentErrorToService (  )
{
    echo "to sent the error information to OT service"
    :
}

xSentNoDetectionToService (   )
{
    echo "sent the no detection information to OT service"
    :
}

xSentOTDetectionToService (  )
{
    echo "sent the OT detection information to OT service"
}

xgetOTinformation (  )
{
        #xgetOTinformation  #obsdate, obstime, telescope, filter, magnitude, ra_real_OTcandidate dec_real_OTcandidate, comimage,pngfile,lightcurve or uplimit
        #how to check the moving obj, with sky coordinates variation? give a plot for it?
        :
}

xgetcatalog (  )
{
# to get the catalog from usno b1.0 from the information of $comimage_astrometry_fit 
    echo "xgetcatalog"
    comimage_astrometry_fit_skycat=`echo $comimage_astrometry_fit | sed 's/\.fit/.skycat/'` 
    comimage_astrometry_fit_xycat=`echo $comimage_astrometry_fit | sed 's/\.fit/.xycat/'` 
    OT_output.xycat=`echo $comimage_astrometry_fit | sed 's/\.fit/_ot.xycat/'`
    OT_output.skycat=`echo $comimage_astrometry_fit | sed 's/\.fit/_ot.skycat/'`
    xy2sky -d $comimage_astrometry_fit 512 512 >center.skycat
    ra_center=`cat center.skycat | awk '{print($1)}'`
    dec_center=`cat center.skycat | awk '{print($2)}'`

    #=============================
    #this part should be helped by han xuhui to read the usnob1.0
    ~/usnob1/usnob1 -m 2000000  -r 0.3 -c $ra_center $dec_center -lmr -6,20 -sr > usnob_out.txt
    #The output is $comimage_astrometry_fit_skycat
    #============================
    sky2xy $comimage_astrometry_fit  @$comimage_astrometry_fit_skycat >$comimage_astrometry_fit_xycat
    #to crossmatch the obj deteced in image and usno b1.0 R2mag
   ./xcross60cmObj2usno #this part is not finished,output is named as $OT_output.xycat
   if test ! -s $OT_output.xycat
   then
       echo "No OT after comparing with USNOB1.0 R2 mag"
       xSentNoDetectionToService
   else
        xy2sky $comimage_astrometry_fit @$OT_output.xycat >$OT_output.skycat 
        xgetOTinformation
        xSentOTDetectionToService

   fi
    
}

xcheckObjdetectionInErrorbox (  )
{
    echo "this part is to check wether any objects  have been detected or not in the error boxes!" 
    ./xcross60cm #output is matchedot.cat, this part is not finished
    if test ! -s matchedot.cat
    then
        echo "no any object is detected in the error box of $xyradius with the center of $x_OT and $y_OT"
        xSentNoDetectionToService
    else
        echo "some objects are detected in the error box of $xyradius with the center of $x_OT and $y_OT"
        xgetcatalog
    fi
}

xcheckOT (  )
{
    echo "xcheckOT"
    ra_OTcandidate=`gethead $FITFILE "ra_mount"`
    dec_OTcandidate=`gethead $FITFILE "dec_mount"`
    sky2xy $FITFILE $ra_mount $dec_mount >OT_xycat
    x_OT=`cat OT_xycat | awk '{printf("%.0f" $1)}'`
    y_OT=`cat OT_xycat | awk '{printf("%.0f" $2)}'`
    echo "The coordinates of OT in the CCD image is :  $x_OT and $y_OT "
    if [  $x_OT > 1024  ] || [ $x_OT < 0   ] || [ $y_OT > 1024  ] || [  $y_OT < 0   ]
    then
        echo "OT is out of the image $FILE, mount pointing is error"
        xSentErrorToService
        continue
    else
        xcheckObjdetectionInErrorbox
    fi
}

xastrometry (   )
{
    if test ! -r xastrometry.flag #showing that xastro.sh is doing
    then
         if test ! -r xastrometry.cat # showing that the astrometry is finished for this field
         then
             echo "xastrometry" 
             if test -r $comimage
             then
                 touch xastrometry.flag 
                 xastro.sh $comimage
                 wait
                 if test -r $comimage_astrometry
                 then
                     echo "astrometry success"
                     mv $comimage_astrometry $comimage_astrometry_fit
                     rm xastrometry.flag
                     xcheckOT
                 else
                     echo "astrometry failed for this image"
                     rm xastrometry.flag
                 fi
             fi
         else
            echo "xastrometry.cat exist, the astrometry is finished for this field" 
         fi
    else
        echo "xastro.sh is doing"
    fi
}


xcombineOnline ( )
{
ID_filter=`gethead $FITFILE_geotran "filter"`  #U V B R I L RED GREEN BLUE EMPTY
stringfilterlistgeotran=`echo listgeotran_$ID_filter`
ls $FITFILE_geotran >>$stringfilterlistgeotran
#ls $FITFILE_geotran >>comlist.new
#if test ! -r comlist.old
#then
#        touch comlist.old
#fi
#diff comlist.old comlist.new | grep  ">" | tr -d '>' | column -t | head -11 >comlist
Ntranfitlist=`wc $stringfilterlistgeotran | awk '{print($1)}'`
if [ $Ntranfitlist -lt 11 ]
then
	echo "$Ntranfitlist is smaller than 11 in $stringfilterlistgeotran" 
	continue
else
	#cat comlist |  column -t >>comlist.old
	comimage=`cat $stringfilterlistgeotran | head -6 | tail -1 | sed 's/\.fit/.com.fit/'`
    comimage_astrometry=`echo $comimage | sed 's/\.com.fit/.new/'` 
    comimage_astrometry_fit=`echo $comimage_astrometry | sed 's/\.new/_new.fit'`
    echo $comimage
	gain=`gethead $fitfile "GAIN" `
    rdnoise=`gethead $fitfile "RDNOISE"`
	ximcombine
    xastrometry  &
fi

}

xbiascheckandcombine (  )
{
    ls $FILE >>listzero
    line_zerolist=`wc -l listzero | awk '{print($1)}'`
    if [ $line_zerolist -gt 10  ] 
    then
        ./xzerocom.sh
        wait
        stringtime=`date -u +%Y%m%d%H%M%S`
        basicstring=`echo "Zero_"$stringtime".fit"`
        cp -f Zero.fit $basicstring
        cp -r Zero.fit $basicstring $dir_basicimage
    fi
}
xflatcheckandcombine (  )
{
    sh xzerocorr.sh
    wait
    ID_filter=`gethead $FILE "filter"`  #U V B R I L RED GREEN BLUE EMPTY
    stringfilterlist=`echo listflat_$ID_filter`
    ls $FILE >>$stringfilterlist
    line_flatlist=`cat $stringfilterlist | wc -l  | awk '{print($1)}'`
    if [ $line_flatlist -gt 4  ] 
    then
        Output_flat=`echo "Flat_$ID_filter"`
        Output_flat_fits=`echo $Output_flat".fit"`
        sh xflatcom.sh $stringfilterlist $Output_flat
        wait
        stringtime=`date -u +%Y%m%d%H%M%S`
        basicstring=`echo $Output_flat"_"$stringtime".fit"`
        cp -f $Output_flat_fits $basicstring
        cp -f $Output_flat_fits $basicstring $dir_basicimage
    fi
}

xnewskyfield (  )
{

            echo "new sky field"
            cp newimageCoord newimageCoord.list 
            touch newpoint.flag
            rm xastro.flag  #this flag shows that astrometry is done
            ls $FILE >listref
            sh xmktemp.sh  # to make the ref.fit and ref.cat
            wait
            continue

}

xobjectreduction (   )
{
    ID_filter=`gethead $FILE "filter"`
    flatname=`echo "Flat_"$ID_filter".fit"`
    sh xzeroflatcorr.sh  $FILE $flatname 
    wait
    ra1=`gethead $FILE "RA-TEL"`
    dec1=`gethead $FILE "DEC-TEL"`
    ra_mount=`skycoor -d $ra1 $dec1 | awk '{printf("%.0f\n",$1)}'`
    dec_mount=`skycoor -d $ra1 $dec1 | awk '{printf("%.0f\n",$2)}'`
    echo $ra_mount $dec_mount $ID_filter >newimageCoord
    if test -s newimageCoord.list
    then
        RaLast=`cat newimageCoord.list | awk '{print($1)}'`
        DecLast=`cat newimageCoord.list | awk '{print($1)}'`
        if [ "$RaLast"  != "$ra_mount"   ] || [  "$DecLast" != "dec_mount"  ]
        then
            xnewskyfield
        else
            echo "This sky field is continuing"
            cp newimageCoord newimageCoord.list 
            #ls $FILE >listg
            xtrackfield #the point is continuing
        fi
    else
        xnewskyfield
    fi


}



xtrackfield (   )
{

	FITFILE=$FILE
	OUTPUT=`echo $FITFILE | sed 's/\.fits/.fit.sex/'`
	OUTPUT_new=`echo $FITFILE | sed 's/\.fits/.fit.sexnew/'`	
	inprefix=`echo $FITFILE | sed 's/\.fits//'`
    imagetmp1sd=`echo $FITFILE | sed 's/\.fits/.fit.mattmp1sd/'`
    imagetransform1sd=`echo $FITFILE | sed 's/\.fits/.fit.transform1sd/'`
	OUTPUT_geoxytran1=`echo $FITFILE | sed 's/\.fits/.fit.tran/'`
	FITFILE_geotran=`echo $FITFILE | sed 's/\.fits/_geo.fit/'`
#	imshift=`echo $FITFILE | sed 's/\.fit/.shift/'`
#        xNpixel=`gethead $FITFILE "NAXIS1"`
#        yNpixel=`gethead $FITFILE "NAXIS2"`
#	xrNpixel=`gethead $referimage "NAXIS1"`
#	yrNpixel=`gethead $referimage "NAXIS2"`
##================================================
	echo $OUTPUT
	sex $FITFILE  -c  daofind.sex  -DETECT_THRESH 5 -ANALYSIS_THRESH 5 -CATALOG_NAME $OUTPUT  
	Nstar=`wc -l $OUTPUT | awk '{print($1)}'`
	echo $Nstar
	if [ $Nstar -lt 5 ]
	then
		echo $FITFILE "starnum is:"$Nstar >>xMiss.list
        echo "The num. of object in this image is too small "
        xSentErrorToService
		continue
	fi
	cp $OUTPUT $OUTPUT_new
	head -1 $OUTPUT_new
	tail -1 $OUTPUT_new
#================================================
	cd $HOME/iraf
	cp -f login.cl.old login.cl
	echo noao >> login.cl
	echo digiphot >> login.cl
	echo daophot >>login.cl
	echo "cd $DIR_data" >> login.cl
	echo flpr >> login.cl
#================================================
	echo $OUTPUT_new $tempmatchstars
	rm -rf mattmp transform.db
	echo "xyxymatch(\"$OUTPUT_new\",\"$tempmatchstars\", \"mattmp\",toleranc=20, xcolumn=1,ycolumn=2,xrcolum=1,yrcolum=2,separation=30, matchin=\"triangles\", inter-,verbo-) " >>login.cl
	echo "geomap(\"mattmp\", \"transform.db\", transfo=\"first\", xmin=1, xmax=1024, ymin=1, ymax=1024,fitgeom=\"general\", functio=\"polynomial\",xxorder=2,xyorder=2,xxterms=\"half\",yxorder=2,yyorder=2,yxterms=\"half\",verbos-, inter-)" >>login.cl
	echo "geoxytran(\"$OUTPUT\", \"$OUTPUT_geoxytran1\",\"transform.db\", transfo=\"first\",geometr=\"geometric\",directi=\"backward\",xcolumn=1,ycolumn=2,calctyp=\"double\",min_sig=7)" >>login.cl
	echo "geotran(\"$FITFILE\",\"$FITFILE_geotran\",\"transform.db\",transfo=\"first\")" >>login.cl
	echo logout >> login.cl
	cl < login.cl >xlogfile.log 
#	cl < login.cl
#================================================
	cd $HOME/iraf
	cp -f login.cl.old login.cl
#================================================
	cd $DIR_data
	mv mattmp $imagetmp1sd
        mv transform.db $imagetransform1sd
	rm -rf mattmp transform.db
#	gethead $FITFILE "date" >timelist.cat
	if test ! -s $imagetransform1sd
	then
		echo $FITFILE "no transform.db" >>xMiss.list
		continue 
	fi
	cat $imagetransform1sd | grep "rms" | awk '{print($2)}'
	xrms=`cat $imagetransform1sd | grep "rms" | awk '{print($2)}' | head -1`
	echo "xrms="$xrms
	if [  `echo " $xrms > 1.0  " | bc ` -eq 1 ]
	then
		continue
	fi
#	cat $imagetransform1sd | grep "shift" | awk '{print($2)}' |  tr '\n' ' ' >coord
#	echo >>coord
#	paste timelist.cat coord $imagetransform1sd >$imshift
	cat $imagetransform1sd | grep "shift" | awk '{print($2)}' |  tr '\n' ' ' >newxyshift.cat
	echo  >>newxyshift.cat
	if test -r newxyshift.cat.all
	then
		cat newxyshift.cat.all newxyshift.cat >temp
		mv temp newxyshift.cat.all
	else
		cp newxyshift.cat newxyshift.cat.all
	fi
	echo "========================" 
	#cat $imshift 
	#echo '  '
	#echo "========================"
	######xMountTrackplot
#	xMountTrack
	xcombineOnline
	rm -rf $OUTPUT $OUTPUT_new $imagetmp1sd $imagetransform1sd $imshift
	echo "Reduction is over for this image"
}

while :
do
    if test ! -r oldlist
    then
        touch oldlist
    fi
    ls *.fits >newlist
     if [ $linenewimage -eq 0   ]
     then
         sleep 2
         continue
     fi
     diff oldlist newlist | grep ">" | tr -d '>' | column -t  >listmatch1
     line=`cat listmatch1 | wc -l`
     if [ "$line" -ne 0  ]
     then
         echo "new image exits"
         head -1 listmatch1 >listmatch
         FILE=`cat listmatch`
         echo $FILE >>oldlist
         ID_ccdtype=`gethead $FILE "imagetyp"`
         if [  "$ID_ccdtype"x = "ZERO"x  ]
         then
            xbiascheckandcombine  #if it is a bias image
        elif [  "ID_ccdtype"x = "FLAT"x   ]
        then
            xflatcheckandcombine  #if it is a flat image
        else
            xobjectreduction      # if it is an object image
        fi
    fi
done
